package com.shelpe.services.sinbad.controller;

import java.util.Date;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.shelpe.services.sinbad.dto.BalanceRtrDto;
import com.shelpe.services.sinbad.service.rtr.BalanceService;
import com.shelpe.services.sinbad.service.rtr.CostService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "/sinbadservice")
@Api("实时数据相关的API")
public class RealtimeReportController {
	@Autowired
	private BalanceService balanceService;
	@Autowired
	private CostService costService;

	@ApiOperation("实时获取账户余额信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "user_id", value = "淘宝用户id", required = true, paramType = "path", dataType = "long"),
			@ApiImplicitParam(name = "time", value = "同步间隔时间,0：即时同步,单位：分钟", defaultValue = "0", required = false, paramType = "query", dataType = "int") })
	@RequestMapping(path = "/users/{user_id}/reports/@realtime_balance", method = RequestMethod.GET)
	public BalanceRtrDto getAccountBalance(@PathVariable("user_id") long userID,
			@RequestParam(name = "time", required = false, defaultValue = "0") int time) {
		BalanceRtrDto dto = new BalanceRtrDto();
		Date queryTime = DateUtils.addMinutes(new Date(), -time);

		BalanceRtrDto mongoPo = this.balanceService.findBalance(userID, queryTime,time);
		if (mongoPo != null && mongoPo.getBalance() != null) {
			dto.setBalance(mongoPo.getBalance());
		} else {
			BalanceRtrDto resDto = this.balanceService.syncBalance(userID,time);
			if (resDto != null) {
				dto.setBalance(resDto.getBalance());
			}
		}
		return dto;
	}

	@ApiOperation("获取账户实时报表数据")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "user_id", value = "淘宝用户id", required = true, paramType = "path", dataType = "long"),
			@ApiImplicitParam(name = "time", value = "同步间隔时间,0：即时同步,单位：分钟", defaultValue = "0", required = false, paramType = "query", dataType = "int") })
	@RequestMapping(path = "/users/{user_id}/reports/@realtime_cost", method = RequestMethod.GET)
	public com.shelpe.services.sinbad.dto.EffectMetricDto getRealtimeCost(@PathVariable("user_id") long userID,
			@RequestParam(name = "time", required = false, defaultValue = "0") int time) {
		com.shelpe.services.sinbad.dto.EffectMetricDto dto = new com.shelpe.services.sinbad.dto.EffectMetricDto();
		Date queryTime = DateUtils.addMinutes(new Date(), -time);

		com.shelpe.services.sinbad.dto.EffectMetricDto mongoPo = this.costService.findCost(userID, queryTime,time);
		if (mongoPo != null) {
			BeanUtils.copyProperties(mongoPo, dto);
		} else {
			com.shelpe.services.sinbad.dto.EffectMetricDto resDto = this.costService.syncCost(userID,time);
			if (resDto != null) {
				BeanUtils.copyProperties(resDto, dto);
			}
		}
		return dto;
	}
}
